package com.customization.assets.formmode.remind;

import com.customization.assets.util.AbstractCronJob;
import com.customization.assets.util.AssetRemind;
import com.customization.assets.util.DBTools;
import com.customization.assets.util.DateTools;
import com.customization.commons.LogTool;
import com.sun.star.logging.SimpleLogRing;
import weaver.conn.RecordSet;
import weaver.soa.workflow.request.RequestService;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Huangwm
 * @description 实物资产字段缺失提醒 主要为Mac字段缺失 特定设备类型下的资产才需要 提醒责任人,科室管理员,部门管理员；科室管理员和部门管理员分别提醒汇总数量即可 不在使用
 */
public class FieldMissingRemind extends AbstractCronJob {

    private LogTool log = new LogTool("/log/assetlog/huangwm/FieldMissingRemind",false);
    private DateTools dateTools = new DateTools();
    private DBTools dbTools = new DBTools();
    private  AssetRemind assetRemind = new AssetRemind();

    public String remindKsgly = "";
    public String getremindKsgly(){
        return remindKsgly;
    }
    public void setremindKsgly(String remindKsgly){
        this.remindKsgly = remindKsgly;
    }
    public String remindBmgly = "";
    public String getremindBmgly(){
        return remindBmgly;
    }
    public void setremindBmgly(String remindBmgly){
        this.remindBmgly = remindBmgly;
    }
    @Override
    public void start() throws Exception {
        log.writeLog("实物资产字段缺失提醒开始 "+dateTools.getCurrentTimes());
        List<Map<String,String>> fieldMissList = getMacFieldMissList();
        List<Map<String,String>> fieldMissCountToZrr = getMacFieldMissCountToZrr();
        //责任人提醒部分
        if(fieldMissCountToZrr.size()>0 && fieldMissList.size()>0){
            //先循环责任人统计获取的实物资产
            for (Map<String,String> map1 : fieldMissCountToZrr){
                String zrr = map1.get("zrr");
                Integer sl = Integer.parseInt(map1.get("sl"));
                List<String> ids = new ArrayList<>();
                for (Map<String,String> map2 : fieldMissList){
                    String id = map2.get("id");
                    String thiszrr = map2.get("zrr");
                    if(zrr.equals(thiszrr)){
                        ids.add(id);
                    }
                }
                try {
                    Map<String,String> mainData = new HashMap<String, String>();
                    mainData.put("txr", "1");
                    mainData.put("tzr", zrr);
                    mainData.put("bz", dbTools.getLastname(zrr)+" 您好,您当前有"+sl+"条资产需要登记Mac地址,请及时补充该信息! <br />可点击相关进入资产信息界面！");
                    mainData.put("xgzc", ids.toString().replace("[", "").replace("]", ""));
                    mainData.put("xglc", "-1");
                    mainData.put("xgbm", "");
                    RequestService service = new RequestService();
                    String requestid = service.createRequest(assetRemind.setRequestInfo("1", "设备Mac信息缺少提醒[责任人] "+dateTools.getCurrentDate(), mainData));
                    log.writeLog("提醒责任人："+dbTools.getLastname(zrr)+"提醒成功 requestid；"+requestid);
                }catch (Exception e){
                    e.printStackTrace();
                    log.writeLog("发送提醒异常"+e.getMessage());
                }
            }
        }
        //科室管理员提醒
        if(remindKsgly.equals("1")){
            List<Map<String,String>> fieldMissCountToSyks = getMacFieldMissCountToSyks();
            if(fieldMissCountToSyks.size()>0){
                for (Map<String,String> map3 : fieldMissCountToSyks){
                    String syks = map3.get("syks");
                    Integer sl = Integer.parseInt(map3.get("sl"));
                    String ksgly = getAsstesAdmin_ks(syks);
                    if(!"".equals(ksgly)){
                        try {
                            Map<String,String> mainData = new HashMap<String, String>();
                            mainData.put("txr", "1");
                            mainData.put("tzr", ksgly);
                            mainData.put("bz", "您当前科室有"+sl+"条资产需要登记Mac地址,系统已默认通知责任人,请跟进该事项!");
                            mainData.put("xgzc", "");
                            mainData.put("xglc", "-1");
                            mainData.put("xgbm", "");
                            RequestService service = new RequestService();
                            String requestid = service.createRequest(assetRemind.setRequestInfo("1", "设备Mac信息缺少提醒[科室管理员] "+dateTools.getCurrentDate(), mainData));
                            log.writeLog("提醒科室管理员："+ksgly+" 提醒成功 requestid；"+requestid);
                        }catch (Exception e){
                            e.printStackTrace();
                            log.writeLog("发送提醒异常"+e.getMessage());
                        }
                    }else{
                        log.writeLog("科室："+dbTools.getDepNameById(syks)+"未设置科室管理员");
                    }
                }
            }
        }
        //部门管理员提醒
        if(remindBmgly.equals("1")){
            List<Map<String,String>> fieldMissCountToDybm = getMacFieldMissCountToDybm();
            if(fieldMissCountToDybm.size()>0){
                for (Map<String,String> map4 : fieldMissCountToDybm){
                    String dybm = map4.get("dybm").toString();
                    Integer sl = Integer.parseInt(map4.get("sl"));
                    String bmgly = getAssetsAdmin_bm(dybm);
                    if(!"".equals(bmgly)){
                        try {
                            Map<String,String> mainData = new HashMap<String, String>();
                            mainData.put("txr", "1");
                            mainData.put("tzr", bmgly);
                            mainData.put("bz", "您当前部门有"+sl+"条资产需要登记Mac地址,系统已默认通知责任人,请跟进该事项!");
                            mainData.put("xgzc", "");
                            mainData.put("xglc", "-1");
                            mainData.put("xgbm", dybm);
                            RequestService service = new RequestService();
                            String requestid = service.createRequest(assetRemind.setRequestInfo("1", "设备Mac信息缺少提醒[部门管理员] "+dateTools.getCurrentDate(), mainData));
                            log.writeLog("提醒部门管理员："+bmgly+" 提醒成功 requestid；"+requestid);
                        }catch (Exception e){
                            e.printStackTrace();
                            log.writeLog("发送提醒异常"+e.getMessage());
                        }
                    }else{
                        log.writeLog("部门："+dbTools.getSubNameById(dybm)+"未设置部门管理员");
                    }
                }
            }
        }
        log.writeLog("实物资产字段缺失提醒结束 "+dateTools.getCurrentTimes());
    }

    /**
     * 获取实物资产中所有缺少Mac地址的资产信息
     * @return
     */
    public List<Map<String,String>> getMacFieldMissList(){
        List<Map<String,String>> list = new ArrayList<>();
        RecordSet rs = new RecordSet();
        String sql = "select id,zcmc,swzcbh,zclx,macdz,zrr,syks,dybm from uf_swzczmxx where zclx not in (21,14,13)  and (macdz is null or macdz ='')";
        rs.execute(sql);
        while (rs.next()){
            Map<String,String> map = new HashMap<>();
            map.put("id", rs.getString("id"));
            map.put("zcmc", rs.getString("zcmc"));
            map.put("swzcbh", rs.getString("swzcbh"));
            map.put("zclx", rs.getString("zclx"));
            map.put("zrr", rs.getString("zrr"));
            map.put("syks", rs.getString("syks"));
            map.put("dybm", rs.getString("dybm"));
            list.add(map);
        }
        log.writeLog("获取实物资产中所有缺少Mac地址的资产总数 "+list.size());
        return list;
    }

    /**
     * 按责任人统计获取实物资产中所有缺少Mac地址的资产数量
     * @return
     */
    public List<Map<String,String>> getMacFieldMissCountToZrr(){
        List<Map<String,String>> list = new ArrayList<>();
        RecordSet rs = new RecordSet();
        String sql = "select zrr,count(1) as sl from uf_swzczmxx where zclx  not in (21,14,13)  and (macdz is null or macdz ='') group by zrr";
        rs.execute(sql);
        log.writeLog(sql);
        while (rs.next()){
            Map<String,String> map = new HashMap<>();
            map.put("zrr", rs.getString("zrr"));
            map.put("sl", rs.getString("sl"));
            list.add(map);
        }
        log.writeLog("按责任人统计获取实物资产缺少mac地址的资产数量结果 "+list);
        return list;
    }

    /**
     * 按使用科室统计获取实物资产中所有缺少Mac地址的资产数量
     * @return
     */
    public List<Map<String,String>> getMacFieldMissCountToSyks(){
        List<Map<String,String>> list = new ArrayList<>();
        RecordSet rs = new RecordSet();
        String sql = "select syks,count(1) as sl from uf_swzczmxx where zclx not in (21,14,13)  and (macdz is null or macdz ='') group by syks";
        rs.execute(sql);
        log.writeLog(sql);
        while (rs.next()){
            Map<String,String> map = new HashMap<>();
            map.put("syks", rs.getString("syks"));
            map.put("sl", rs.getString("sl"));
            list.add(map);
        }
        log.writeLog("按科室统计获取实物资产缺少mac地址的资产数量结果 "+list);
        return list;
    }

    /**
     * 按使用部门统计获取实物资产中所有缺少Mac地址的资产数量
     * @return
     */
    public List<Map<String,String>> getMacFieldMissCountToDybm(){
        List<Map<String,String>> list = new ArrayList<>();
        RecordSet rs = new RecordSet();
        String sql = "select dybm,count(1) as sl from uf_swzczmxx where zclx not  in (21,14,13)  and (macdz is null or macdz ='') group by dybm";
        rs.execute(sql);
        log.writeLog(sql);
        while (rs.next()){
            Map<String,String> map = new HashMap<>();
            map.put("dybm", rs.getString("dybm"));
            map.put("sl", rs.getString("sl"));
            list.add(map);
        }
        log.writeLog("按部门统计获取实物资产缺少mac地址的资产数量结果 "+list);
        return list;
    }

    /**
     * 获取部门资产管理员
     * @param dybm
     * @return
     */
    public String getAssetsAdmin_bm(String dybm){
        RecordSet rs = new RecordSet();
        String assetsAdmin = "";
        String sql = "select swzcgly from Matrixtable_1 where id="+dybm;
        rs.execute(sql);
        while(rs.next()){
            assetsAdmin = rs.getString("swzcgly");
        }
        return assetsAdmin;
    }

    /**
     * 获取科室管理员
     * @param syks
     * @return
     */
    public String getAsstesAdmin_ks(String syks){

        RecordSet rs = new RecordSet();
        String assetsAdmin = "";
        String sql = "select swzcgly from Matrixtable_2 where id="+syks;
        rs.execute(sql);
        while(rs.next()){
            assetsAdmin = rs.getString("swzcgly");
        }
        return assetsAdmin;
    }
}
